﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Medical_Store_Billing.Component;
using System.Windows.Forms;
using Medical_Store_Billing.Object_Class;

namespace Medical_Store_Billing.Data_Access_Layer
{
    class PhieunhapDAL
    {
        Dataservice m_phieunhapdal = new Dataservice();


        public DataTable Laychitietphieunhap(String maphieu)
        {
            SqlCommand cmd = new SqlCommand("select * from chitietphieunhap where maphieunhap='" + maphieu + "'");
            m_phieunhapdal.Load(cmd);
            
            return m_phieunhapdal;
           
        }

// danh sach phieu nhap hang
        public DataTable Laydsphieunhap()
        {
            SqlCommand cmd = new SqlCommand("Select * from phieunhap");
            
            m_phieunhapdal.Load(cmd);
            
            return m_phieunhapdal;
        }


//tim kiem phieu nhap hang
        public DataTable Timphieunhap(string maphieu)
        {
            SqlCommand cmd = new SqlCommand("Select * from phieunhap where maphieunhap like N'%" + maphieu + "%'");
            m_phieunhapdal.Load(cmd);
            return m_phieunhapdal;
        }


//lap phieu nhap 
        public bool Themphieunhap(phieunhapclass m_pn)
        {
            SqlCommand cmd = new SqlCommand("Insert into phieunhap values(@maphieunhap,@ngaynhap,@tongtien,@manv)");
            cmd.Parameters.Add("maphieunhap", SqlDbType.VarChar).Value = m_pn.maphieunhap;
            cmd.Parameters.Add("ngaynhap", SqlDbType.SmallDateTime).Value = m_pn.ngaynhap;
            cmd.Parameters.Add("tongtien", SqlDbType.Float).Value = m_pn.tongtien;
            cmd.Parameters.Add("manv", SqlDbType.VarChar).Value = m_pn.manv;
            

            if (m_phieunhapdal.ExecuteNonQuery(cmd) > 0)
            {
                MessageBox.Show("Thêm phiếu nhập thành công!");
                return true;
            }
            else return false;
        }

//lap chi tiet phieu nhap
        public bool Themchitietphieunhap(chitietnhapclass m_pn)
        {
            SqlCommand cmd = new SqlCommand("Insert into chitietphieunhap values(@maphieunhap,@mahang,@soluong)");
            cmd.Parameters.Add("maphieunhap", SqlDbType.VarChar).Value = m_pn.maphieunhap;
            cmd.Parameters.Add("mahang", SqlDbType.VarChar).Value = m_pn.mahang;
            cmd.Parameters.Add("soluong", SqlDbType.Int).Value = m_pn.soluong;
            


            if (m_phieunhapdal.ExecuteNonQuery(cmd) > 0)
            {
               
                return true;
            }
            else return false;
        }
        public DataTable Demphieunhap()
        {
            SqlCommand cmd = new SqlCommand("select count(*) from phieunhap");
            m_phieunhapdal.Load(cmd);

            return m_phieunhapdal;
        }
    }
}
